<?php
namespace App\Console\Commands\bmr;

use App\Console\CommandTrait;
use App\Model\FoodModel;
use App\Service\BMR\BMR_Food;
use App\DripEmailer;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class UpdateFoodData extends Command
{
    use CommandTrait;
    /**
     * 命令行的名称及签名。
     *
     * @var string
     */
    protected $signature = 'food:update';

    /**
     * 命令行的描述
     *
     * @var string
     */
    protected $description = '采集食物数据';




    /**
     * 执行命令行。
     *
     * @param  \App\Service\BMR\BMR_Food  $BMR_Food
     * @return mixed
     */
    public function handle(BMR_Food $BMR_Food)
    {
        $this->startLog($this->description);
        //采集数据
        $service = $BMR_Food;
        $data = $service->all();
        $now = Carbon::now();
        $data = collect($data)->map(function($row) use ($now) {
            $row['created_at'] = (string)$now;
            return $row;
        })->toArray();
        if (empty($data)) {
            $this->setLog('无法采集数据');
        } elseif (!FoodModel::insert($data)) {
            //插入数据
            $this->setLog('更新失败');
        }
        $this->endLog($this->description);
    }
}